function Article() {
    this.thumbnailIn = $('#thumbnail-form');
}

/**
 * 点击上传图片事件
 */
Article.prototype.listenUploadEvent = function () {
    var btn = $('#upload-img-btn');
    var self = this;
    btn.change(function () {
        var file = btn[0].files[0];
        var formData = new FormData();
        formData.append('file', file);
        myajax.post({
            url: '/cms/upload_file/',
            data: formData,
            /*上传文件需指定以下两个*/
            processData: false,
            contentType: false,
            success: function (result) {
                if (result['code'] === 200) {
                    var url = result['data']['url'];
                    self.thumbnailIn.val(url);
                }
            }
        });
    });
};

/**
 * 上传图片到聚合图床
 */
Article.prototype.listenJuheUploadEvent = function () {
    var btn = $('#upload-img-btn');
    var self = this;
    var formData = new FormData();
    btn.change(function () {
        self.thumbnailIn.val('正在上传');
        myajax.post({
            url: '/cms/juhesign/',
            success: function (result) {
                if (result['code'] === 200) {
                    var file = btn[0].files[0];
                    var data = result['data'];
                    formData.append('file', file);
                    formData.append('sign', data['sign']);
                    formData.append('id', data['id']);
                    formData.append('ts', data['ts']);

                    $.post({
                        url: 'https://api.superbed.cn/upload',
                        data: formData,
                        /*上传文件需指定以下两个*/
                        processData: false,
                        contentType: false,
                        success: function (result) {
                            if (result['err'] === 0) {
                                self.thumbnailIn.val(result['url']);
                            } else {
                                linalert.alertErrorToast('上传失败，请重试');
                                self.thumbnailIn.val('上传失败，请重试');
                            }
                        }
                    });
                }
            }
        });
    });
};

/**
 * 初始化ueditor编辑器
 */
Article.prototype.initUeditor = function () {
    window.ue = UE.getEditor('editor', {
        serverUrl: '/ueditor/upload/',
        initialFrameHeight: 400
    })
};

/**
 * 点击发布文章事件
 */
Article.prototype.listenSubmitEvent = function () {
    var btn = $('#submit-btn');
    var titleInput = $('input[name="title"]');
    var categoryInput = $('select[name="category"]');
    var descInput = $('input[name="desc"]');
    var thumbnailInput = $('input[name="thumbnail"]');
    var pk = btn.attr('data-article-id');
    var url = '';
    if(pk){
        url = '/cms/edit_article/';
    }else{
        url = '/cms/write_article/';
    }
    btn.click(function (event) {
        event.preventDefault();
        var title = titleInput.val();
        var category = categoryInput.val();
        var desc = descInput.val();
        var thumbnail = thumbnailInput.val();
        var content = window.ue.getContent();
        if (!title) {
            linalert.alertInfo('请输入标题');
            return;
        } else if (!category) {
            linalert.alertInfo('请输入分类');
            return;
        } else if (!desc) {
            linalert.alertInfo('请输入描述');
            return;
        } else if (!thumbnail) {
            linalert.alertInfo('请输入缩略图地址');
            return;
        } else if (!content) {
            linalert.alertInfo('请输入文章内容');
            return;
        }
        myajax.post({
            url,
            data: {
                title,
                category,
                desc,
                thumbnail,
                content,
                pk
            },
            'success': function (result) {
                if (result['code'] === 200) {
                    if (pk){
                        linalert.alertSuccessToast('修改成功');
                    }else{
                        linalert.alertSuccess('文章发表成功', function () {
                            window.location.reload();
                        });
                    }
                }
            }
        });
    })
};

/**
 * 入口
 */
Article.prototype.run = function () {
    //上传图片到自己的服务器上
    //this.listenUploadEvent();
    //上传图片到聚合图床
    this.listenJuheUploadEvent();
    this.initUeditor();
    this.listenSubmitEvent();
};

$(function () {
    var a = new Article();
    a.run();
});
